//
//  SliderController.m
//  gdemm
//
//  Created by 乔明超 on 16/9/24.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "SliderController.h"
#import "TabBarController.h"
#import "LeftController.h"
#import "LoginController.h"
#import "UserDefaultsUtil.h"
#import "ToastManager.h"
#import "InboxManager.h"
#import "MessageManager.h"
#import "HeartBeat.h"
@interface SliderController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIPanGestureRecognizer *pan;
@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UIViewController * centerViewController;
@property (nonatomic, strong) UIViewController * leftDrawerViewController;
@property (strong,nonatomic) NSTimer *timer;
@property (assign,nonatomic) NSInteger mUndownloadedFile;
@property (assign,nonatomic) NSInteger mUnreadMessage;
@property (weak, nonatomic) UITabBarItem *cloudItem;
@property (weak, nonatomic) UITabBarItem *messageItem;
@end
static SliderController *singletonInstance;
@implementation SliderController
#pragma mark - Initialization -

+ (SliderController *)sharedInstance
{
    if (!singletonInstance)
        return nil;
    
    return singletonInstance;
}

- (id)init
{
    if (self = [super init])
    {
        singletonInstance = self;
    }
    
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        singletonInstance = self;
    }
    
    return self;
}

- (UIPanGestureRecognizer *)pan {
    if (!_pan) {
        _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];
        _pan.delegate = self;
    }
    return _pan;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    TabBarController *tabBarVC = [[TabBarController alloc]init];
    LeftController *leftVC = [[LeftController alloc]init];
    
    self.centerViewController = tabBarVC;
    self.leftDrawerViewController = leftVC;
    leftVC.view.x = -(kScreenWidth * (1-kLeftWidthScale));
    self.leftView = leftVC.view;
    [self.view addSubview:leftVC.view];
    [self.view addSubview:tabBarVC.view];
    [tabBarVC.view addGestureRecognizer:self.pan];
    [self timerTask];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerTask) userInfo:nil repeats:YES];
    //添加消息中间件
    [[HeartBeat shareInstance]sendRedis];
    NSLog(@"HeartBeat shareInstance]sendRedis");
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UITabBarController *tabBar = (UITabBarController *)self.centerViewController;
    NSArray *itemArr = [[tabBar tabBar]items];
    self.cloudItem = itemArr[0];
    self.messageItem = itemArr[1];
}
- (void)timerTask{
    //心跳离线
    if ([[UserDefaultsUtil shareInstance]getBool:LC_HBOFFLINE]) {
        [self offline];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window makeToast:@"您的账号在其他设备上登录或密码被更改"];
        
    }
    if ([[UserDefaultsUtil shareInstance]getBool:LC_HBFAILED]) {
        [self offline];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window makeToast:@"您的账号出现异常,请重新登入"];
    }
}

/*用户离线*/
- (void)offline{
    [[HeartBeat shareInstance]offline];
    [self.timer invalidate];
    UIWindow *window =[UIApplication sharedApplication].delegate.window;
    window.rootViewController = nil;
    LoginController *loginVc = [[LoginController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:loginVc];
    window.rootViewController = nav;
}

//手势滑动侧边栏
- (void)handlePanAction:(UIPanGestureRecognizer *)sender {
    //1.获取手指拖拽的时候平移的值
    CGPoint translation = [sender translationInView:sender.view];
    // 2. 让当前视图进行平移
    sender.view.transform = CGAffineTransformTranslate(sender.view.transform, translation.x, 0);
    self.leftView.tx = sender.view.tx / 1.5;
    // 3. 让平移的值不要累加
    [sender setTranslation:CGPointZero inView:sender.view];
    //4.获取最右边的范围
    CGAffineTransform rightScopeTransform = CGAffineTransformTranslate(self.view.transform, kScreenWidth * kLeftWidthScale, 0);
    if (sender.view.tx > rightScopeTransform.tx) {
        // 当移动到右边极限时
        // 限制最右边的范围
        sender.view.transform = rightScopeTransform;
        self.leftView.tx = sender.view.tx / 1.5;
    } else if (sender.view.tx < 0.0) {
        // 限制最左边的范围
        sender.view.transform = CGAffineTransformTranslate(self.view.transform, 0, 0);
        self.leftView.tx = sender.view.tx / 1.5;
    }
    // 拖拽结束时
    if (sender.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.2f animations:^{
            if (sender.view.left > kScreenWidth * 0.5) {
                sender.view.transform = rightScopeTransform;
                self.leftView.tx = sender.view.tx / 1.5;
            } else {
                sender.view.transform = CGAffineTransformIdentity;
                self.leftView.tx = sender.view.tx / 1.5;
            }
        }];
    }
}

//左侧点击滑动侧边栏
- (void)leftTapHandler{
    
        [UIView animateWithDuration:0.2f animations:^{
    
            if (self.centerViewController.view.tx == kScreenWidth * kLeftWidthScale) {
                self.centerViewController.view.transform = CGAffineTransformTranslate(self.centerViewController.view.transform, -kScreenWidth * kLeftWidthScale, 0);
                self.leftView.tx = self.tabBarController.view.tx / 1.5;
    
            }else{
                self.centerViewController.view.transform = CGAffineTransformTranslate(self.centerViewController.view.transform, kScreenWidth * kLeftWidthScale, 0);
                self.leftView.tx = self.centerViewController.view.tx / 1.5;
            }
    
        }];
    
        CGAffineTransform rightScopeTransform = CGAffineTransformTranslate([kAppDelegate window].transform, kScreenWidth * kLeftWidthScale, 0);
        if (self.centerViewController.view.tx > rightScopeTransform.tx) {
            // 当移动到右边极限时
            // 限制最右边的范围
            self.centerViewController.view.transform = rightScopeTransform;
            self.leftView.tx = self.centerViewController.view.tx / 1.5;
        } else if (self.centerViewController.view.tx < 0.0) {
            // 限制最左边的范围
            self.centerViewController.view.transform = CGAffineTransformTranslate([kAppDelegate window].transform, 0, 0);
            self.leftView.tx = self.centerViewController.view.tx / 1.5;
        }

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint point = [touch locationInView:self.view];
    if (point.x < 200 || self.centerViewController.view.x == kScreenWidth * kLeftWidthScale) {
        return YES;
    }
    return  NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
